package _interview150;

/**
 * 151. 反转字符串中的单词
 */
public class No151 {
    public String reverseWords(String s) {
        StringBuilder sb = new StringBuilder();
        int n = s.length();
        int start = n - 1;
        while (start >= 0) {
            while (start >= 0 && s.charAt(start) == ' ') start--;
            int end = start + 1;
            while (start >= 0 && s.charAt(start) != ' ') start--;
            sb.append(s, start + 1, end);
            sb.append(" ");
        }
        String ans = sb.toString();
        int right = ans.length() - 1;
        while (right >= 0 && ans.charAt(right) == ' ') right--;
        return ans.substring(0, right + 1);
    }
}
